﻿/** 
 * Copyright 2010 NUMKIT.COM (contact: opensource@numkit.com)
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 * 
 * @author 陈超
 * @version 1.0
 */
class Data {
	public var aPolyline:Array;
	public function Data(node:XMLNode) {
		if (node.nodeName != "data") {
			throw new Error("Not A data Node!");
		}
		aPolyline = new Array();
		var nodeName:String;
		for (var child:XMLNode = node.firstChild; child != null; child=child.nextSibling) {
			nodeName = child.nodeName;
			if (nodeName == "polyline") {
				var id:String=child.attributes["id"];
				if(id==undefined){
					throw new Error("Can't find id attribute!");
				}
				var color:Number = Number(child.attributes["color"]);
				if (color == undefined || isNaN(color)) {
					throw new Error("Can't find color attribute or invalid!");
				}
				var lineWidth:Number = Number(child.attributes["lineWidth"]);
				if (isNaN(lineWidth) || lineWidth<0) {
					lineWidth = 1;
				}
				var xBased:Boolean = false;
				var xNumBase:Number = Number(child.attributes["xNumBase"]);
				if (!isNaN(xNumBase) && xNumBase != 0) {
					xBased = true;
				}
				var yBased:Boolean = false;
				var yNumBase:Number = Number(child.attributes["yNumBase"]);
				if (!isNaN(yNumBase) && yNumBase != 0) {
					yBased = true;
				}
				var pArray:Array = new Array();
				for (var grandson:XMLNode = child.firstChild; grandson != null; grandson=grandson.nextSibling) {
					nodeName = grandson.nodeName;
					if (nodeName == "p") {
						var x:Number = Number(grandson.firstChild.firstChild.nodeValue);
						var y:Number = Number(grandson.lastChild.firstChild.nodeValue);
						if (x == undefined || isNaN(x)) {
							throw new Error("Can't find x node or invalid!");
						}
						if (y == undefined || isNaN(y)) {
							throw new Error("Can't find y node or invalid!");
						}
						if (xBased) {
							x += xNumBase;
						}
						if (yBased) {
							y += yNumBase;
						}
						pArray.push(new Point(x, y));
					}
				}
				aPolyline.push(new Polyline(id, color, lineWidth, pArray));
			}
		}
	}
}
